package com.grablist.feihu.grablist.ui.widget;


import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

import com.grablist.feihu.grablist.R;


/**
 * 基本的提示框显示
 *
 * @since 2014-12-23
 */
public class BAlertDialog implements OnClickListener {
    private android.app.AlertDialog alertDialog;
    private TextView titleView;// 提示
    private TextView messageView;// 信息
    private TextView positiveButton;// 确定
    private TextView negativeButton;// 取消
    private OnClickListener positiveClickListener, negativeButtonClickListener;

    public BAlertDialog(Context context) {
        alertDialog = new android.app.AlertDialog.Builder(context).create();
        alertDialog.setCancelable(false);
        alertDialog.setCanceledOnTouchOutside(true);// false 设置点击屏幕Dialog不消失
        alertDialog.show();
        Window window = alertDialog.getWindow();
        window.setContentView(R.layout.balertdialog);
        titleView = (TextView) window
                .findViewById(R.id.tv_dialog_layout_confirm_title_auth);// 标题
        messageView = (TextView) window
                .findViewById(R.id.cb_rember_statue_auth);// 内容
        positiveButton = (TextView) window
                .findViewById(R.id.tv_dialog_layout_confirm_auth);// 确定
        negativeButton = (TextView) window
                .findViewById(R.id.tv_dialog_layout_cancel_auth);// 取消
        positiveButton.setOnClickListener(this);
        negativeButton.setOnClickListener(this);
    }

    public BAlertDialog setTitle(String title) {
        titleView.setText(title);
        return this;
    }

    public BAlertDialog setMessage(String message) {
        messageView.setText(message);
        return this;
    }

    // 设置肯定按钮
    public BAlertDialog setPositiveButton(String text,
                                          final OnClickListener listener) {
        positiveButton.setText(text);
        positiveClickListener = listener;
        positiveButton.setOnClickListener(this);
        positiveButton.setVisibility(View.VISIBLE);
        return this;
    }

    /**
     * 设置否定按钮
     *
     * @param
     * @param listener
     * @return
     */
    public BAlertDialog setNegativeButton(String text,
                                          final OnClickListener listener) {
        negativeButton.setText(text);
        negativeButtonClickListener = listener;
        negativeButton.setOnClickListener(this);
        negativeButton.setVisibility(View.VISIBLE);
        return this;
    }

    /**
     * 单独设置一个按钮
     *
     * @param text
     * @param listener
     * @return
     */
    public BAlertDialog setSimpleButton(String text,
                                        final OnClickListener listener) {
        positiveButton.setText(text);
        positiveClickListener = listener;
        positiveButton.setOnClickListener(this);
        positiveButton.setVisibility(View.VISIBLE);
        negativeButton.setVisibility(View.GONE);
        return this;
    }

    public void show() {
        alertDialog.show();
    }

    public void dismiss() {
        alertDialog.dismiss();
    }

    public boolean isShowing() {
        return alertDialog.isShowing();
    }

    public void onClick(View v) {
        alertDialog.dismiss();
        if (positiveClickListener != null
                && v.getId() == R.id.tv_dialog_layout_confirm_auth) {
            positiveClickListener.onClick(v);
        }
        if (negativeButtonClickListener != null
                && v.getId() == R.id.tv_dialog_layout_cancel_auth) {
            negativeButtonClickListener.onClick(v);
        }
    }

}
